<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-tree
                        :data="sortData"
                        node-key="id"
                        ref="sortTree"
                        default-expand-all
                        :expand-on-click-node="false"
                        @node-click="hdlClick"
                        >
                    <span class="custom-tree-node" slot-scope="{ node, data }">
                       <span>{{ node.label }}</span>
                       <span>
                         <el-button v-show="data.label!='系统菜单'" icon="el-icon-delete" type="text" size="mini" @click.stop="rowDelete(data)"></el-button>
                       </span>
                     </span>
                </el-tree>
            </el-col>
            <el-col :span="16">
                <div class="container">
                    <div class="handle-box">
                        <el-form :model="query" ref="queryForm" :inline="true" label-width="68px">
		                    <el-form-item label="菜单名称" prop="name">
                                <el-input v-model="query.name" placeholder="菜单名称" size="small" class="handle-input mr10"></el-input>
                            </el-form-item>
                             <el-form-item>
                                <el-button type="cyan" icon="el-icon-search"  size="mini"  @click="handleSearch">搜索</el-button>
                            </el-form-item>
                        </el-form>
                        <div>
                            <el-row :gutter="10" class="mb8 el-form-item">
                                <el-col :span="1.5">
                                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="addSysRole">新增</el-button>
                                </el-col>
                            </el-row>
		                </div>
                    </div>
                    <el-table
                            :data="tableData"
                            border
                            class="table"
                            ref="multipleTable"
                            header-cell-class-name="table-header"
                            @selection-change="handleSelectionChange"
                    >
                        <el-table-column type="selection" width="55" align="center"></el-table-column>
                        <el-table-column type="index" label="ID" width="55" align="center" ></el-table-column>
                        <el-table-column prop="name" label="菜单名称"></el-table-column>
                        <el-table-column prop="sysName" label="系统名"></el-table-column>
                        <el-table-column prop="icon" label="图标"></el-table-column>
                        <el-table-column prop="url" label="地址"></el-table-column>
                        <el-table-column label="操作" width="180" align="center">
                            <template slot-scope="scope">
                                <el-button
                                        type="text"
                                        icon="el-icon-edit"
                                        @click="handleEdit(scope.$index, scope.row)"
                                >编辑</el-button>
                                <el-button
                                        type="text"
                                        icon="el-icon-delete"
                                        class="red"
                                        @click="handleDelete(scope.$index, scope.row)"
                                >删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <el-pagination
                                background
                                layout="total, prev, pager, next"
                                :current-page="query.pageIndex"
                                :page-size="query.pageSize"
                                :total="pageTotal"
                                @current-change="handlePageChange"
                        ></el-pagination>
                    </div>
                </div>
            </el-col>
        </el-row>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="菜单名称" prop="name" >
                    <el-input maxlength="20" v-model="form.name" ></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon" >
                    <el-input maxlength="20" v-model="form.icon" ></el-input>
                </el-form-item>
                <el-form-item label="系统名称" prop="sysName">
                    <el-input maxlength="20" v-model="form.sysName" ></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="url">
                    <el-input maxlength="20" v-model="form.url" ></el-input>
                </el-form-item>
                <el-form-item label="排序"  prop="sort" >
                    <el-input maxlength="2" v-model="form.sort" ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="butCancel">取 消</el-button>
                <el-button type="primary" @click="saveEdit('form')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import sysMenu from '../../api/sysMenu';
    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    name: '',
                    status:'',
                    id:'',
                    parentId:'',
                    version:'',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                menuList:[],
                editVisible: false,
                pageTotal: 0,
                form: {
                    parentId:"1",
                    name:"",
                    icon:"",
                    sysName:"",
                    url:"",
                    sort:""
                },
                rules:{
                    name :[{required: true, message: '菜单名称不能为空', trigger: 'blur'}],
                    icon :[{required: true, message: '图标不能为空', trigger: 'blur'}],
                    sysName :[{required: true, message: '系统名称不能为空', trigger: 'blur'}],
                },
                idx: -1,
                id: -1,
                // 树
                sortData: [],
                defaultProps: {
                    children: "children",
                    label: "label"
                },
            };
        },
        created() {
            this.getData();
        },
        filters:{
            //格式化后台传过来的时间戳
            dateFormat(time) {
                let date = new Date(time);
                return vue.$tool._dateFormat(date,'yyyy-MM-dd hh:mm')
            }
        },
        methods: {
            // 获取list
            getData() {
                var _that =this;
                sysMenu.selectPage(this.query).then(res => {
                    _that.tableData = res.data.items;
                    _that.pageTotal = res.data.total || 50;
                });
                sysMenu.selectList({}).then(res => {
                    _that.toTreeData(res.data.items, 'id', 'parentId', 'name');
                });
            },
            // 查询角色
            selectSysMenuOne(id){
                var _that =this;
                sysMenu.selectSysMenu(id).then(res => {
                    _that.form = res.data;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 树删除
            rowDelete(data){
                this.handleDelete(0,data)
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    var param ={
                        status:1,
                        id:row.id,
                        version: row.version
                    }
                    sysMenu.delSysMenu(param).then(res => {
                        this.$message.success('删除成功');
                        this.getData();
                    });
                })
                .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.editVisible = true;
                this.selectSysMenuOne(row.id)
            },
            //新增操作
            addSysRole() {
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit(formData) {
                this.$refs[formData].validate((valid) => {
                    if (valid) {
                        if(this.form.id != null && this.form.id != undefined){
                            // 编辑
                            sysMenu.updateSysMenu(this.form).then(res => {
                                this.$message.success("保存成功");
                                this.getData();
                            });
                        }else{
                            // 新增
                            sysMenu.addSysMenu(this.form).then(res => {
                                this.$message.success("保存成功");
                                this.getData();
                            });
                        }
                        this.butCancel();
                    }
                });
            },
            butCancel(){
                this.editVisible = false;
                this.form = {
                    name:"",
                    icon:"",
                    sysName:"",
                    url:"",
                    sort:""
                }
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            // 树单击
            hdlClick(r){
                this.query.parentId = r.id ;
                this.form.parentId = r.id ;
                // 判断是否父级菜单
                if( this.form.parentId == 1){
                    this.form.isParent = 1
                }else{
                    this.form.isParent = 0
                }
                this.getData();
            },
            toTreeData(data, id, pid, name) {
                // 建立个树形结构,需要定义个最顶层的父节点，pId是1
                let parent = [];
                for (let i = 0; i < data.length; i++) {
                    if (data[i][pid] != "" && data[i][pid] != null) {
                    } else {
                        let obj = {
                            label: data[i][name],
                            id: data[i][id],
                            version:data[i]['version'],
                            children: []
                        };
                        parent.push(obj); //数组加数组值
                    }
                }
                this.children(parent,data, id, pid, name);
            },
            children(parent,data, id, pid, name) {

                let _this = this;
                if (data) {
                    for (let i = 0; i < parent.length; i++) {
                        for (let j = 0; j < data.length; j++) {
                            if (parent[i].id == data[j][pid]) {
                                var prentList = [];
                                let obj = {
                                    label: data[j][name],
                                    id: data[j][id],
                                    version:data[j]['version'],
                                    children: []
                                };
                                prentList.push(obj);
                                _this.children(prentList,data, id, pid, name);
                                parent[i].children.push(obj);
                            }
                        }
                        _this.children(parent[i].children);
                    }
                }
                _this.sortData = parent
            }
        },
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .floatr {
        float: right;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
    .custom-tree-node{
        flex: 1;
        display:flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        padding-right: 8px;
    }
</style>
